package com.pjnkj.thunder.dagger.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pjnkj.thunder.dagger.common.Sheath;
import com.pjnkj.thunder.dagger.common.SpringUtils;
import com.pjnkj.thunder.entity.Checklist;
import com.pjnkj.thunder.mapper.SqlMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.pjnkj.thunder.dagger.common.Arc.parameter4http;

public class CheckService extends Sheath {

    public CheckService(Object o) {
        String field = parameter4http(o, "field");
        String table = parameter4http(o, "table");
        SqlMapper sqlMapper = SpringUtils.getBean("sqlMapper");
        if (sqlMapper != null) {
            List<String> alltable = sqlMapper.getTableAll(this.getDatabaseName());
            for (String name : alltable) {
                //如果查询到表
                if (name.equals(table)) {
                    ArrayList<HashMap> nameAndChecks = new ArrayList<HashMap>();
                    ArrayList<String> existlist = new ArrayList<String>();
                    //如果没穿字段参数，返回所有字段校验规则，如果有传，返回相应字段校验字段规则
                    if (field.equals("")) {
                        HashMap<String, Object> aliasmap = new HashMap<String, Object>() {{
                            put("alias", sqlMapper.getLineAllEx(table, "tablename", "alias"));
                        }};
                        HashMap<String, Object> effectmap = new HashMap<String, Object>() {{
                            put("effect", sqlMapper.getLineAllEx(table, "tablename", "effect"));
                        }};
                        nameAndChecks.add(aliasmap);
                        nameAndChecks.add(effectmap);
                        for (String fieldname : sqlMapper.getFieldNameAll(this.getDatabaseName(), table)) {
                            QueryWrapper qwcheck = new QueryWrapper();
                            qwcheck.eq("tablename", table);
                            qwcheck.eq("field", fieldname);
                            Checklist checklist = new Checklist();
                            Checklist checklistone = checklist.selectOne(qwcheck);
                            if (checklistone != null) {
                                HashMap<String, Object> map0 = new HashMap<String, Object>() {{
                                    put("name", fieldname);
                                    //这里加入字段校验数据
                                    put("checklist", checklistone);
//                                    List<HashMap> test = alias.stream().filter((e) ->
//                                            e.get("field").equals(fieldname)
//                                    ).collect(Collectors.toList());
//                                    if (test.size() > 0) {
//                                        put("alisa", test.get(0).get("name"));
//                                    }
                                }};
                                nameAndChecks.add(map0);
                            }
                        }
                    } else {
                        HashMap alias = sqlMapper.getLinetwo(table, "tablename", field, "field", "alias");
                        QueryWrapper qwcheck2 = new QueryWrapper();
                        qwcheck2.eq("tablename", table);
                        qwcheck2.eq("field", field);
                        Checklist checklist = new Checklist();
                        Checklist checklistone = checklist.selectOne(qwcheck2);
                        if (checklistone != null) {
                            HashMap<String, Object> map0 = new HashMap<String, Object>() {{
                                put("name", table);
                                //这里加入字段校验数据
                                put("checklist", checklistone);
                                if (alias != null) {
                                    put("alisa", alias.get("name"));
                                }
                            }};
                            nameAndChecks.add(map0);
                        }
                    }
                    this.setObject(nameAndChecks);
                    break;
                }
            }
        }

    }

}
